package weavingdraft;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;

/**
 * Creates the ThreadingSquare object allowing users to interact with. 
 * @author Justin Wong
 *
 */
@SuppressWarnings("serial")
public class ThreadingSquare extends JPanel{
	/**
	 * The WeavingDraft this object refers to. 
	 */
	private WeavingDraft my_draft;
	/**
	 * The row this object refers to. 
	 */
	private int my_row;
	/**
	 * The column this object refers to. 
	 */
	private int my_column;
	/**
	 * The ThreadGrid this object refers to. 
	 */
	private ThreadGrid my_thread_panel;
	/**
	 * Checks for double clicks. 
	 */
	private boolean double_click;
	/**
	 * The number of clicks to help determine if this needs to be black or white. 
	 */
	int number;
	/**
	 * Creates the ThreadingSquare object. 
	 * @param the_row the row this object refers to. 
	 * @param the_column the column this object refers to. 
	 * @param the_draft the WeavingDraft this object refers to. 
	 * @param the_grid the ThreadingGrid this obejct refers to. 
	 */
	public ThreadingSquare(int the_row, int the_column, WeavingDraft the_draft, ThreadGrid the_grid){
		my_draft = the_draft;
		my_row = the_row;
		my_column = the_column;
		my_thread_panel = the_grid;
		double_click = false;
		number = 0;
		this.addMouseListener(new MouseAction(this));
	}
	/**
	 * Repaints the component when selected. 
	 */
	@Override
	public void paintComponent(Graphics the_graphics){
		Graphics2D variable_graphics = (Graphics2D) the_graphics;
		int width = this.getWidth();
		int height = this.getHeight();

		if(number % 2 == 0){
			if(double_click){
				my_thread_panel.setSquare(null, my_column);
				double_click = false;
				my_draft.deleteThread(my_row, my_column);
			}
			variable_graphics.setColor(Color.WHITE);
			variable_graphics.fillRect(0, 0, width - 1, height - 1);
			variable_graphics.setColor(Color.BLACK);
			variable_graphics.drawRect(0, 0, width - 1, height - 1);
		}else{
			if(my_thread_panel.getSquare(my_column) != null){
				ThreadingSquare s = my_thread_panel.getSquare(my_column);
				s.addCount();
				s.repaint();
			}
			my_draft.setThreading(my_row, my_column);
			my_thread_panel.setSquare(this, my_column);
			
			variable_graphics.setColor(Color.BLACK);
			variable_graphics.fillRect(0, 0, width - 1, height - 1);
			variable_graphics.setColor(Color.WHITE);
			variable_graphics.drawRect(0, 0, width - 1, height - 1);
		}
	}
	/**
	 * Updates the count to determine if the color needs to be changed. 
	 */
	public void addCount(){
		number++;
		repaint();
	}
	/**
	 * Gets the row number this object has. 
	 * @return the row number. 
	 */
	public int getRow(){
		return my_row;
	}
	/**
	 * Attaches the action to this panel. 
	 * @author Justin Wong
	 *
	 */
	class MouseAction extends MouseInputAdapter{
		/**
		 * The JPanel this object is referred to. 
		 */
		private JPanel my_panel;
		/**
		 * Creates the object and how to attach it to. 
		 * @param the_panel the Object MouseAction refers to. 
		 */
		private MouseAction(JPanel the_panel){
			my_panel = the_panel;
		}
		/**
		 * Determines the action when the object is clicked. 
		 */
		@Override
		public void mouseClicked(MouseEvent e){
			number++;
			ThreadingSquare s = my_thread_panel.getSquare(my_column);
			if(s != null && s.getRow() == my_row){
				double_click = true;
			}
			my_panel.repaint();
		}
	}

}
